<template>
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    :rules="rules"
    label-position="left"
    label-width="0px"
  >
    <el-form-item prop="username">
      <el-input
        v-model="ruleForm.username"
        type="text"
        maxlength="11"
        auto-complete="off"
        placeholder="请输入账户"
        class="iptHeight"
        style="border-radius: 8px; border: 1px solid #ddd; background: rgba(255, 255, 255, 0.3);"
      />
    </el-form-item>

    <el-form-item prop="password">
      <el-input
        v-model="ruleForm.password"
        maxlength="20"
        type="password"
        class="iptHeight"
        auto-complete="off"
        placeholder="请输入密码"
        @keyup.enter="handleLogin"
        style="border-radius: 8px; border: 1px solid #ddd; background: rgba(255, 255, 255, 0.3);"
      />
    </el-form-item>

    <div class="boxBottom">
  <el-checkbox
    v-model="ruleForm.rememberMe"
  >自动登录</el-checkbox>

  <div class="forgetPsd" @click="() => $emit('forgetPassword')">忘记密码</div>
</div>

    <el-form-item style="width: 100%">
      <el-button
        :loading="loading.btns"
        type="primary"
        style="width: 100%; border-radius: 27px; height: 54px; background-color: #409EFF; border: none;"
        @click.native.prevent="handleLogin"
      >
        <span v-if="!loading.btns">登录</span>
        <span v-else>登 录 中...</span>
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { defineComponent, reactive, ref } from 'vue';
import { useStore } from 'vuex';
import { UserActionEnum } from '@/store/modules/user/state';
import { useRouter } from 'vue-router';

import { encrypt } from '@/utils/rsaEncrypt.js'

defineComponent({
  name: 'AccountLoginComponent'
});
defineEmits(['forgetPassword']);

const store = useStore();
const router = useRouter();
let ruleForm = reactive({
  username: '',
  password: '',
  rememberMe: false,
})

let loading = reactive({
  btns: false,
})

const rules = {
  username: [
    { required: true, trigger: 'blur', message: '用户名不能为空' },
  ],
  password: [
    { required: true, trigger: 'blur', message: '密码不能为空' },
  ],
}

const ruleFormRef = ref();
function handleLogin () {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      store.dispatch(UserActionEnum.LOGIN, {
        username: encrypt(ruleForm.username),
        password: encrypt(ruleForm.password),
        rememberMe: ruleForm.rememberMe,
      })
        .then(() => {
          router.push({ path: '/' }).then(() => {
            window.location.reload()
          })
        })
        .catch(() => {
        }).finally(() => {
          loading.btns = false;
        })
    } else {
      return false
    }
  })
}

</script>

<style lang="scss" scoped>
.boxBottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  
  .forgetPsd {
    font-size: 14px;
    color: #3A68F2;
    cursor: pointer;
  }

  .el-form-item {
    margin-bottom: 20px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .iptHeight {
    height: 48px;
    font-size: 14px;
  }

  .boxBottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .forgetPsd {
      color: #409EFF;
      cursor: pointer;
      font-size: 14px;
      transition: all 0.3s ease;

      &:hover {
        color: #66b1ff;
        text-decoration: underline;
      }
    }
  }

  .el-button {
    width: 100%;
    height: 54px;
    border-radius: 27px;
    font-size: 16px;
    font-weight: 500;
    letter-spacing: 1px;
    background-color: #409EFF;
    border: none;
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);

    &:hover {
      background-color: #66b1ff;
      transform: translateY(-2px);
    }

    &:active {
      transform: translateY(0);
    }
  }
}

// 控制输入框宽度
:deep(.el-form-item) {
  width: 90%;
  margin: 0 auto 20px;
  
  &:last-child {
    margin-bottom: 0;
  }
}
</style>
